home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / Util.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  1.9 KB  |  82 lines

  1. package symantec.itools.awt.util;
  2.  
  3.  
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics;
  7. import java.awt.Toolkit;
  8. import java.awt.Image;
  9. import java.awt.Component;
  10.  
  11.  
  12. /**
  13.  *
  14.  *
  15.  * @version 1.0, Nov 26, 1996
  16.  *
  17.  * @author    Symantec
  18.  *
  19.  */
  20.  
  21. //     04/11/97    LAB    Added the getGraphics(Image image, Component component) function
  22. //     04/20/97    LAB    Added import statements for Image and Component.
  23. //                Fixed a type-o in the getGraphics function I added previously that
  24. //                prevented it from working properly.
  25.  
  26. public class Util
  27. {
  28.     public static int getFontHeight(Graphics g)
  29.     {
  30.         return getFontHeight(g.getFontMetrics());
  31.     }
  32.  
  33.     public static int getFontHeight(Font f)
  34.     {
  35.         return getFontHeight(Toolkit.getDefaultToolkit().getFontMetrics(f));
  36.     }
  37.  
  38.     public static int getFontHeight(FontMetrics m)
  39.     {
  40.         return m.getHeight();
  41.     }
  42.  
  43.     public static int getStringWidth(Graphics g, String s)
  44.     {
  45.         return getStringWidth(g.getFontMetrics(), s);
  46.     }
  47.  
  48.     public static int getStringWidth(Font f, String s)
  49.     {
  50.         return getStringWidth(Toolkit.getDefaultToolkit().getFontMetrics(f), s);
  51.     }
  52.  
  53.     public static int getStringWidth(FontMetrics m, String s)
  54.     {
  55.         return m.stringWidth(s);
  56.     }
  57.  
  58.     public static Font getDefaultFont()
  59.     {
  60.         return new Font("Dialog", 12, Font.PLAIN);
  61.     }
  62.  
  63.     //getGraphics
  64.     /**
  65.      * Preserves the font information of a graphics object retrieved from
  66.      * an image so that susequent calls to getFontMetrics through the graphics
  67.      * object will not result in a NullPointerException.
  68.      */
  69.     public static Graphics getGraphics(Image image, Component component)
  70.     {
  71.         if(image == null)
  72.             return null;
  73.  
  74.         Graphics graphics = image.getGraphics();
  75.         if(graphics != null && component != null && component.getFont() != null)
  76.         {
  77.             graphics.setFont(component.getFont());
  78.         }
  79.         return graphics;
  80.     }
  81. }
  82.